<template>
  <el-dialog width="600px" title="修改计件工资" append-to-body :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="submitForm.date"
          type="date"
          placeholder="请选择日期"
          value-format="yyyy-MM-dd"
          clearable
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="员工名称" prop="employeeName">
        <el-input v-model="submitForm.employeeName" disabled placeholder="请输入员工名称" />
      </el-form-item>
      <el-form-item label="班次" prop="shiftId">
        <el-select v-model="submitForm.shiftId" placeholder="请选择班次" style="width: 100%;">
          <el-option
            v-for="item in shiftList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产量" prop="quantity">
        <el-input v-model="submitForm.quantity" disabled type="number" placeholder="选择日期和班次后自动显示总产量" />
      </el-form-item>
      <el-form-item label="工序" prop="processId">
        <el-select v-model="submitForm.processId" disabled placeholder="请选择工序" style="width: 100%;">
          <el-option
            v-for="item in processList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计件工资" prop="pieceRateWage">
        <el-input v-model="submitForm.pieceRateWage" disabled type="number" placeholder="选择工序后自动显示工序单价" />
      </el-form-item>
      <el-form-item label="补贴">
        <el-input v-model="submitForm.subsidy" type="number" placeholder="请输入补贴" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { selectAllShift } from '@/api/erp/shift'
import { selectAllProcess } from '@/api/erp/process'
import { selectPieceRateWageDetailById, updatePieceRateWageDetailById } from '@/api/financial/pieceRateWage'

export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      formLoading: false,
      shiftList: [],
      processList: [],
      submitForm: {
        id: '',
        date: '',
        employeeId: '',
        shiftId: '',
        quantity: '',
        processId: '',
        pieceRateWage: '',
        subsidy: ''
      },
      submitFormRules: {
        date: [{ required: true, message: '请选择日期', trigger: 'change' }],
        employeeId: [{ required: true, message: '请选择员工', trigger: 'change' }],
        shiftId: [{ required: true, message: '请选择班次', trigger: 'change' }],
        quantity: [{ required: true, message: '请输入产量', trigger: 'change' }],
        processId: [{ required: true, message: '请选择工序', trigger: 'change' }],
        pieceRateWage: [{ required: true, message: '请输入计件工资', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      this.selectAllShift()
      this.selectAllProcess()
      if (!id) {
        return
      }
      this.formLoading = true
      selectPieceRateWageDetailById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          this.formLoading = true
          updatePieceRateWageDetailById(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    selectAllShift() {
      this.formLoading = true
      selectAllShift().then(resp => {
        if (resp.success) {
          this.shiftList = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    selectAllProcess() {
      this.formLoading = true
      selectAllProcess().then(resp => {
        if (resp.success) {
          this.processList = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>
